﻿#include "setwidget.h"

SetWidget::SetWidget(QWidget *parent)
    :QWidget (parent)
{
    this->init_window();
    this->init_control();

    this->init_connect();
    this->init_qss();

    restsizeByte = 500;
}

SetWidget::~SetWidget()
{

}

void SetWidget::init_window()
{
    this->setWindowTitle("设置界面");
    this->resize(700,700);

}

void SetWidget::init_control()
{

    //标签控件
    this->title = new QLabel("设置界面",this);
    this->title->move(300,50);
    this->title->setStyleSheet("font-size:40px;font-weight:bold");
    this->equipment = new QLabel("设备编号", this);
    this->equipment->move(100,150);
    this->setSize = new QLabel("内存空间", this);
    this->setSize->move(100,230);
    this->restSize = new QLabel("内存剩余", this);
    this->restSize->move(100,310);
    this->videopath = new QLabel("录像存储", this);
    this->videopath->move(100,390);
    this->imgpath = new QLabel("图片存储", this);
    this->imgpath->move(100,470);

    //编辑框控件
//    this->equipmentEdit = new QLineEdit("", this);
//    this->equipmentEdit->move(250,150);
//    this->equipmentEdit->setReadOnly(true);
    this->setSizeEdit = new QLineEdit("", this);
    this->setSizeEdit->move(250,220);
    this->setSizeEdit->setReadOnly(true);
    this->restSizeEdit = new QLineEdit("", this);
    this->restSizeEdit->move(250,300);
    this->restSizeEdit->setReadOnly(true);
    this->videopathEdit = new QLineEdit("", this);
    this->videopathEdit->move(250,380);
    this->videopathEdit->setStyleSheet("width:270px");
    this->videopathEdit->setReadOnly(true);
    this->imgpathEdit = new QLineEdit("", this);
    this->imgpathEdit->move(250,460);
    this->imgpathEdit->setReadOnly(true);
    this->imgpathEdit->setStyleSheet("width:270px");

    //按钮控件
    this->enterBtn = new QPushButton("确定", this);
    this->enterBtn->move(150,580);
    this->backBtn = new QPushButton("取消", this);
    this->backBtn->move(450,580);

    //下拉框、选择框
    //视频
    this->videopathChooseBtn = new QPushButton(this);
    this->videopathChooseBtn->setIcon(QIcon(":/img/dir1.png"));
    this->videopathChooseBtn->move(530,385);
    this->videopathChooseBtn->setStyleSheet("border:none;width:30px;height:30px");
    //图片
    this->imgpathChooseBtn = new QPushButton(this);
    this->imgpathChooseBtn->setIcon(QIcon(":/img/dir1.png"));
    this->imgpathChooseBtn->move(530,465);
    this->imgpathChooseBtn->setStyleSheet("border:none;width:30px;height:30px");

    //设备下拉框
    this->equipBox = new QComboBox(this);
    this->equipBox->move(250,140);
    this->equipBox->setStyleSheet("width:280px;height:40px");
    QList<QCameraInfo> camers = QCameraInfo::availableCameras();
    qDebug() << QString("总共个%1个摄像头").arg(camers.size());
    for (int i=0;i<camers.size();i++) {
        this->equipBox->addItem(camers[i].description());
        qDebug() << camers[i].description();
    }

    //计算磁盘空间大小的部分
    //默认定死了500M
    this->setSizeEdit->setText("  500MB");


}

void SetWidget::init_connect()
{
    connect(this->videopathChooseBtn, SIGNAL(clicked()), this, SLOT(chooseVideoPath()));
    connect(this->imgpathChooseBtn, SIGNAL(clicked()), this, SLOT(chooseImagePath()));
    connect(this->enterBtn, SIGNAL(clicked()), this, SLOT(doSet()));
    connect(this->backBtn, SIGNAL(clicked()), this, SLOT(clearForm()));
}

void SetWidget::init_qss()
{
    QFile file(":/img/set.qss");
    if(file.open(QFile::ReadOnly))
    {
 //       qDebug() << "assdasd";
        this->setStyleSheet(file.readAll());
        file.close();
    }
}

qint64 SetWidget::calculateFolderSize(const QString &folderPath)
{
    qint64 size = 0;
        QDir directory(folderPath);
        QFileInfoList fileList = directory.entryInfoList(QDir::Files | QDir::Dirs | QDir::NoDotAndDotDot);

        foreach(const QFileInfo &fileInfo, fileList) {
            if (fileInfo.isDir()) {
                size += calculateFolderSize(fileInfo.filePath()); // 递归计算子文件夹的大小
            } else {
                size += fileInfo.size(); // 累加文件大小
            }
        }

        return size;
}

void SetWidget::clearForm()
{
    this->imgpathEdit->clear();
    this->videopathEdit->clear();
}

void SetWidget::closeEvent(QCloseEvent *event)
{
    QMessageBox::StandardButton resBtn = QMessageBox::question(this, "Confirm Exit",
                tr("Are you sure you want to exit?\n"),
                QMessageBox::Cancel | QMessageBox::No | QMessageBox::Yes,
                QMessageBox::Yes);

            if (resBtn != QMessageBox::Yes) {
                event->ignore();
            } else {
                UserModel::getInstance()->updateUserAll();
                event->accept();
            }
}

void SetWidget::chooseVideoPath()
{
//    this->folderPath = QFileDialog::getExistingDirectory(nullptr, "选择文件夹", QDir::homePath());
    this->folderPath = new QFileDialog(this);
    this->folderPath->setFileMode(QFileDialog::Directory);
    this->folderPath->exec();
    auto selectDir = this->folderPath->selectedFiles();
    if (selectDir.size()>0)
    {
           qDebug() << "Dir Path:" << selectDir.at(0);
           this->videopathEdit->setText(selectDir.at(0));
    }

    //计算磁盘大小
    qint64 folderSize = calculateFolderSize(this->videopathEdit->text());
    double folderSizeKB = folderSize / 1024.0;
    qDebug() << folderSizeKB;
    //剩余大小默认的500先转换为kb
    restsizeByte *= 1024;
    restsizeByte -= folderSizeKB;
    restsizeByte /= 1024.0;
    this->restSizeEdit->setText("  "+QString::number(restsizeByte, 'f', 2)+"MB");

    //使用后删除因为等会图片路径的选择也是这个变量
    delete this->folderPath;

}

void SetWidget::chooseImagePath()
{
    this->folderPath = new QFileDialog(this);
    this->folderPath->setFileMode(QFileDialog::Directory);
    this->folderPath->exec();
    auto selectDir = this->folderPath->selectedFiles();
    if(selectDir.size()>0)
    {
        qDebug() << "Dir Path:" << selectDir.at(0);
        this->imgpathEdit->setText(selectDir.at(0));
    }



    //计算磁盘大小
    qint64 folderSize = calculateFolderSize(this->imgpathEdit->text());
    double folderSizeKB = folderSize / 1024.0;
    qDebug() << folderSizeKB;
    //剩余大小默认的500先转换为kb
    restsizeByte *= 1024;
    restsizeByte -= folderSizeKB;
    restsizeByte /= 1024.0;
    this->restSizeEdit->setText("  "+QString::number(restsizeByte, 'f', 2)+"MB");
    //使用后删除，可能会在视频路径选择的时候继续使用
    delete this->folderPath;
}

void SetWidget::doSet()
{
    if(this->equipBox->count()==0)
    {
        QMessageBox::warning(this, "设置界面", "设备信息不能为空");
        return ;
    }
    if(this->videopathEdit->text().isEmpty()||this->imgpathEdit->text().isEmpty())
     {
        QMessageBox::warning(this, "设置界面", "请选择完整的存储路径");
        return ;
    }
    int res = ConfigureModel::getInstance()->InsertConfigure(this->videopathEdit->text(),this->imgpathEdit->text(), 500, this->equipBox->currentText());
    if(res==0)
    {
        QMessageBox::information(this,"设置界面", "设置成功");
        this->clearForm();
        //设置成功后new主界面
        this->mainwidget = new MainWidget();
        this->mainwidget->show();

        this->mainwidget->getPlaycode()->start();
        connect(this->mainwidget, SIGNAL(backToSet()), this, SLOT(showSet()));
        this->hide();
        SingletonBD::getInstance()->closedb();

        return ;
    }
    else {
        QMessageBox::warning(this, "设置界面", "设置失败");
        return ;
    }
}

void SetWidget::showSet()
{
    this->show();
    this->mainwidget->hide();
}

